package com.dcsgo.v1ch04;

import java.time.*;

/**
 * 
 * @ClassName: CalendarTest
 * @Description: 打印本月日历
 * @author DCSGO
 * @date 2023-11-12 10:31:38
 */
public class CalendarTest {
	public static void main(String[] args) {
		LocalDate date = LocalDate.now();
		int month = date.getMonthValue();
		int today = date.getDayOfMonth();

		date = date.minusDays(today - 1);// 将date设为当前月的第一天
		DayOfWeek weekday = date.getDayOfWeek();
		int weekdayValue = weekday.getValue();// 1->星期一 2->星期二 ... 7->星期日

		System.out.println("Mon Tue Wed Thu Fri Sat Sun");
		// 当前月的第一天前要空weekdayValue-1个位置(哪些星期数是上个月的)
		for (int i = 1; i < weekdayValue; i++) {
			System.out.print("    ");
		}
		// 打印本月的每一天 今天标记*
		while (date.getMonthValue() == month) {

			System.out.printf("%3d", date.getDayOfMonth());
			if (date.getDayOfMonth() == today) {
				System.out.print("*");
			} else {
				System.out.print(" ");
			}
			date = date.plusDays(1);
			//当前输出的天数的下一天是周一则打印换行
			if (date.getDayOfWeek().getValue() == 1) {
				System.out.println();
			}
		}
		//当前天是不是周一我们需要换行
		if (date.getDayOfWeek().getValue() != 1) {
			System.out.println();
		}
	}

}
